import time
from time import sleep

from selenium.webdriver.common.by import By

from base.base import Base

url = 'http://192.168.8.222/admin.php'


class WarehousePage1(Base):
    # 获取cookies
    def login(self):
        self.save_cookie('../data/w_warehouse_cookies.json')
        sleep(20)

    # 跳过登录
    def login_admin(self):
        self.set_cookie('../data/w_warehouse_cookies.json')
        self.driver.get(url)
        self.click((By.XPATH, '//*[@id="admin-offcanvas"]/div/ul/li[10]/a'))
        # 点击商品管理，进入iframe
        self.click((By.XPATH, '//*[@id="power-menu-438"]/li[2]/a'))
        self.go_iframe((By.ID, "ifcontent"))

    def add_commodity(self):
        # 点击添加
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/button[1]'))
        # 点击输入框，默认仓库
        self.click((By.XPATH, '//*[@id="warehouse-goods-popup"]/div/div[2]/form/div[1]/div[1]/a'))
        self.click((By.XPATH, '//*[@id="warehouse-goods-popup"]/div/div[2]/form/div[1]/div[1]/div/ul/li[8]'))
        # 商品分类,手机
        self.click((By.XPATH, '//*[@id="warehouse-goods-popup"]/div/div[2]/form/div[1]/div[2]/a'))
        self.click((By.XPATH, '//*[@id="warehouse-goods-popup"]/div/div[2]/form/div[1]/div[2]/div/ul/li[4]'))
        # 输入手机
        self.input((By.XPATH, '//*[@id="warehouse-goods-popup"]/div/div[2]/form/div[1]/input'), '手机')
        # 搜索并添加
        self.click((By.XPATH, '//*[@id="warehouse-goods-popup"]/div/div[2]/form/div[1]/button'))
        self.click((By.XPATH, '//*[@id="warehouse-goods-popup"]/div/div[2]/form/div[2]/ul/li[1]/div/div/a'))
        sleep(1)
        self.click((By.XPATH, '//*[@id="warehouse-goods-popup"]/div/div[1]/span[2]'))
        print(self.get_text((By.XPATH, '//*[@id="common-prompt"]/p')))
        return self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))

    def delete_commodity(self):
        sleep(1)
        # 勾选
        self.click((By.CSS_SELECTOR, 'td:nth-child(1) > div > label'))
        # 点击删除按钮
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/button[2]'))
        self.click((By.CSS_SELECTOR, 'div.am-modal-footer > span:nth-child(2)'))
        sleep(3)
        print(self.get_text((By.XPATH, '//*[@id="common-prompt"]/p')))
        self.click((By.XPATH, '//*[@id="warehouse-goods-popup"]/div/div[1]/span[2]'))
        return self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))

    def reset_search_commodity(self, commodity):
        # 输入商品型号
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[2]/input'), commodity)
        # 选择仓库
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[3]/div/ul/li/input'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[3]/div/div/ul/li[1]'))
        # 点击搜索
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/button[3]'))
        sleep(1)
        # 点击重置
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/a'))
        sleep(1)

    def sidebar_commodity(self, inventory):
        # 点击查看详情
        self.click((By.CSS_SELECTOR,
                    'td.am-nowrap-initial.am-text-middle.am-text-center.am-grid-fixed-right.am-operate-grid.am-grid-fixed-right-shadow > button.am-btn.am-btn-default.am-btn-xs.am-radius.am-btn-block.submit-popup'))
        # 点击x
        self.click((By.XPATH, '//div[starts-with(@id,"am-modal-")]/div/span'))
        # 点击库存
        self.click((By.CSS_SELECTOR,
                    'td.am-nowrap-initial.am-text-middle.am-text-center.am-grid-fixed-right.am-operate-grid.am-grid-fixed-right-shadow > button.am-btn.am-btn-secondary.am-btn-xs.am-radius.am-btn-block.submit-popup'))
        # self.click((By.XPATH, '//div[starts-with(@id,"am-modal-")]/div/span'))
        # 编辑库存
        self.go_iframe((By.XPATH, '//div[starts-with(@id,"am-modal-")]/div/iframe'))
        # self.input((By.XPATH, '/html/body/div[2]/div/form/table/tbody/tr/td[2]/input[1]'), inventory)
        # # 批量设置
        self.click((By.XPATH, '//*[@id="inventory-dropdown"]'))
        self.input((By.XPATH, '//*[@id="inventory-dropdown"]/div/div/input'), inventory)
        # # 点击确认
        self.click((By.XPATH, '//*[@id="inventory-dropdown"]/div/div/span/button'))
        # # 退出编辑
        self.click((By.XPATH, '/html/body/div[2]/div/form/div/button'))
        print(self.get_text((By.XPATH, '//*[@id="common-prompt"]/p')))
        sleep(2)
        # 删除
        self.driver.switch_to.default_content()
        self.go_iframe((By.ID, "ifcontent"))
        self.click((By.CSS_SELECTOR,
                    'button.submit-delete'))
        self.click((By.CSS_SELECTOR, 'div.am-modal-footer > span:nth-child(2)'))
        print(self.get_text((By.XPATH, '//*[@id="common-prompt"]/p')))
        return self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))


if __name__ == '__main__':
    w = WarehousePage1(url)
    # w.login()
    w.login_admin()
    # w.add_commodity()
    # w.delete_commodity()
    w.reset_search_commodity('苹果')
    w.sidebar_commodity('100')
